package com.cino.product.service.impl;

import com.cino.basic.base.service.BaiduAiAuditService;
import com.cino.basic.util.AjaxResult;
import com.cino.product.domain.Product;
import com.cino.product.domain.ProductDetail;
import com.cino.product.domain.ProductOnlineAuditLog;
import com.cino.product.mapper.ProductDetailMapper;
import com.cino.product.mapper.ProductMapper;
import com.cino.product.mapper.ProductOnlineAuditLogMapper;
import com.cino.product.domain.Product;
import com.cino.basic.base.service.impl.BaseServiceImpl;
import com.cino.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
     * 重写添加方法，添加产品和产品明细
     * @param product
     */
    @Override
    @Transactional
    public void save(Product product) {
        super.save(product);
        //获取宠物详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            //添加宠物详情
            productDetailMapper.save(productDetail);
        }
    }

    /**
     * 重写删除方法，删除产品和产品明细
     * @param id
     */
    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        productDetailMapper.delDetailByProductId(id);
    }

    /**
     * 重写更新方法，更新产品和产品明细
     * @param product
     */
    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }
    }

    /**
     * 重写批量删除方法，删除产品和产品明细
     * @param ids
     */
    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    /**
     * 上架的方法
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids){
            Product product = productMapper.loadById(id);
            Boolean boo1= baiduAiAuditService.textAudit(product.getName());
            Boolean boo2= baiduAiAuditService.imageAudit(product.getResources());
            if(boo1 && boo2){//通过
                //改状态和上架时间， 以及添加审核日志
                productMapper.updateOnsaleState(id,new Date());

                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(1);
                log.setNote("审核成功");
                productOnlineAuditLogMapper.save(log);
            }else{
                //添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(0);
                log.setNote("审核失败");
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }

    /**
     * 下架的方法
     * @param ids
     */
    @Override
    @Transactional
    public void offsale(List<Long> ids) {
        //改状态 state = 0; 下架时间 new Date()
        //批量更新，多参数操作 +Param ,mapper里面加
        productMapper.updateOffsaleState(ids,new Date());

    }
}
